from PyQt4 import QtGui,QtCore
from ftllibs.FTL import RUNES
from ftllibs.ui.UI_Runes_Menu import RunesMenu
from ftllibs.FTL import RunesDS

class RunesTab(QtGui.QWidget):
    def __init__(self,parent=None):
        super(RunesTab,self).__init__()
        self.MasterLayout = QtGui.QGridLayout()
        self.setLayout(self.MasterLayout)
        self.unsaved=False
        self.NewRunes=True
        self.parent=parent
        self.initUI()

    def initUI(self):
        self.RunesMenu = RunesMenu()
        self.MasterLayout.addWidget(self.RunesMenu)
        self.view = QtGui.QListView()
        self.model=RunesDS.GetRunesDataModel()
        self.view.setModel(self.model)
        self.view.setFixedWidth(300)
        self.view.setContextMenuPolicy(3) # Qt.CustomContextMenu
        self.RunesDisplay = RunesDisplay()
        self.RunesNameLabel = QtGui.QLabel('<h1>New Unsaved Runes</h1>')
        self.AddRunesButton=QtGui.QPushButton('Add')
        self.SaveRunesButton=QtGui.QPushButton('Save')
        self.ButtonLayout=QtGui.QGridLayout()
        self.ButtonLayout.addWidget(self.AddRunesButton,0,0,1,2)
        self.ButtonLayout.addWidget(self.SaveRunesButton,1,0,1,2)
        self.MasterLayout.addLayout(self.ButtonLayout,0,0,1,1)
        self.MasterLayout.addWidget(self.view,1,0,2,1)
#        self.MasterLayout.addWidget(self.Build,1,2,1,1,QtCore.Qt.AlignHCenter)
 #       self.MasterLayout.addWidget(self.NumberedStats,2,2,1,1)
        self.MasterLayout.addWidget(self.RunesDisplay,0,1,2,1)
        self.MasterLayout.addWidget(self.RunesMenu,0,2,3,1)
        self.AddRunesButton.clicked.connect(self.AddRunes)
        self.SaveRunesButton.clicked.connect(self.SaveRunes)

    def CustomContext(self,pos):
        index=self.view.indexAt(pos)
        if index.isValid():
            self.ContextMenu.exec_(self.view.mapToGlobal(pos))
        else:
            self.AddContextMenu.exec_(self.view.mapToGlobal(pos))

    def ClearRunes(self):
#        self.Champ.ClearBuild()
        self.unsaved=True

    def Update(self):
        pass

    def AddRunes(self):
        text,ok = QtGui.QInputDialog.getText(self,'Add New Rune Page','Name your Layout:',text='Unnamed Runes')
        if ok:
            lastrow=self.model.rowCount()
            print "AddRunes: lastrow: ", lastrow
            self.model.insertRows(lastrow,1,text)
            index = self.model.createIndex(lastrow,0)
            self.view.setCurrentIndex(index)
            if self.NewRunes:
                self.NewRunes=False
                self.SaveRunes()
                self.LoadRunes()
            else:
                self.LoadRunes()

    def SetChampLevel(self,lvl):
        self.Champ.SetChampLevel(lvl)
        self.NumberedStats.UpdateStats()

    def SetChampMasteries(self,masteriesID):
        MID=self.MasteriesModel.getMID(masteriesID)
        masteries=self.MasteriesModel.loadMasteries(MID)
        self.Champ.SetMasteries(masteries)
        self.NumberedStats.UpdateStats()

    def DeleteRunes(self):
        ok = QtGui.QMessageBox.question(self,'Delete Runes',"Do you realy want to delete\nInsert Runes name here\n","Yes","No")
        if ok == 0:
            index = self.view.currentIndex()
            self.model.removeRows(index.row(),1)

    def SaveRunes(self):
        if self.NewRunes:
            self.AddRunes()
        else:
            build = self.Runes.getRunes()
            self.model.saveRunes(build)
            self.unsaved=False

    def LoadRunes(self):
        self.CheckSave()
#        self.Runes.clearRunes()
        index=self.view.currentIndex()
        RID=self.model.getRID(index.row())
#        self.Runes.setRunes(self.model.loadRunes(RID))
        self.RunesNameLabel.setText(self.Runes.Name)
        self.NewRunes=False
        self.unsaved=False

    def RequestClose(self):
        print "RunesTab -> RequestClose called"
        self.close()

    def CheckSave(self):
        if self.unsaved:
            ok = QtGui.QMessageBox.question(self,'Save Changes ?',"Do you want to save your changes ?","Yes","No")
            if ok == 0:
                self.SaveRunes()


class RunesDisplay(QtGui.QWidget):
    def __init__(self,parent=None):
        super(RunesDisplay,self).__init__()
        self.RunesBG=QtGui.QPixmap('images/ui/RunesBG.png')
        self.setFixedSize(self.RunesBG.size())
        self.InitRunePositions()
        self.InitRunes()

    def InitRunes(self):
        self.Runes={}
        self.Runes['mark1']=None
        self.Runes['mark2']=None
        self.Runes['mark3']=None
        self.Runes['mark4']=None
        self.Runes['mark5']=None
        self.Runes['mark6']=None
        self.Runes['mark7']=None
        self.Runes['mark8']=None
        self.Runes['mark9']=None
        
        self.Runes['seal1']=None
        self.Runes['seal2']=None
        self.Runes['seal3']=None
        self.Runes['seal4']=None
        self.Runes['seal5']=None
        self.Runes['seal6']=None
        self.Runes['seal7']=None
        self.Runes['seal8']=None
        self.Runes['seal9']=None

        self.Runes['glyph1']=None
        self.Runes['glyph2']=None
        self.Runes['glyph3']=None
        self.Runes['glyph4']=None
        self.Runes['glyph5']=None
        self.Runes['glyph6']=None
        self.Runes['glyph7']=None
        self.Runes['glyph8']=None
        self.Runes['glyph9']=None

        self.Runes['quintessence1']=None
        self.Runes['quintessence2']=None
        self.Runes['quintessence3']=None

    def InitRunePositions(self):
        self.RunePositions = {}
        self.RunePositions['mark1']=QtCore.QPoint(29,417)
        self.RunePositions['mark2']=QtCore.QPoint(103,417)
        self.RunePositions['mark3']=QtCore.QPoint(192,417)
        self.RunePositions['mark4']=QtCore.QPoint(10,353)
        self.RunePositions['mark5']=QtCore.QPoint(84,352)
        self.RunePositions['mark6']=QtCore.QPoint(151,365)
        self.RunePositions['mark7']=QtCore.QPoint(35,287)
        self.RunePositions['mark8']=QtCore.QPoint(135,300)
        self.RunePositions['mark9']=QtCore.QPoint(90,250)
        
        self.RuneDropRectangles = {}
        for runename,point in self.RunePositions.iteritems():
            self.RuneDropRectangles[runename]=QtCore.QRect(self.RunePositions[runename],QtCore.QSize(64,64))

    def paintEvent(self,e):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(0,0,self.RunesBG)
        for rune,point in self.RunePositions.iteritems():
            img = QtGui.QPixmap(64,64)
            img.loadFromData(RUNES.RUNES['r_m__resilience'].IconData)
            painter.drawPixmap(point,img)

        for rune,rec in self.RuneDropRectangles.iteritems():
            painter.drawRect(rec)